//
//  TestGUI.cpp
//  NanoOSX
//
//  Created by Jeppe Nielsen on 9/8/13.
//  Copyright (c) 2013 Jeppe Nielsen. All rights reserved.
//

#include "Engine.hpp"
#include "GameState.hpp"
#include "GameWorld.hpp"
#include "RenderSystem.hpp"
#include "Touchable.hpp"
#include "Material.hpp"
#include "TextureComponent.hpp"
#include "FirstPersonMoverSystem.hpp"
#include "TouchSystem.hpp"
#include "Light.hpp"
#include "SpriteMeshSystem.hpp"
#include "LabelMeshSystem.hpp"
#include "TransformHierarchy.hpp"
#include "SpriteTextureSystem.hpp"

using namespace Nano;

class TestGUI : public GameState<TestGUI> {
    void Initialize() {
        Input.TouchUp += event_handler(this, &TestGUI::TouchUp);
        renderer = world.CreateSystem<RenderSystem>();
        world.CreateSystem<FirstPersonMoverSystem>()->Input = &Input;
        world.CreateSystem<TouchSystem>()->Input = &Input;
        world.CreateSystem<SpriteMeshSystem>();
        world.CreateSystem<LabelMeshSystem>();
        world.CreateSystem<TransformHierarchy>();
        world.CreateSystem<SpriteTextureSystem>();
        
        GameObject* atlas = world.CreateObject();
        atlas->AddComponent<Atlas>()->Load("images.xml", 1024);
        atlas->AddComponent<TextureComponent>()->Texture().LoadFromPng("images.png");
        
        GameObject* camera = world.CreateObject();
        camera->AddComponent<Camera>()->Viewport = Manager().Viewport();
        camera->AddComponent<Transform>()->Position = Vector3(0,0,4);
        camera->AddComponent<FirstPersonMover>();
        
        gadget = world.CreateObject();
        gadget->AddComponent<Transform>()->Position = Vector3(0,0,-0.05f);
        gadget->AddComponent<Mesh>();
        gadget->AddComponent<Material>()->BlendMode = BlendMode::Alpha;
        gadget->AddComponent<Sizeable>()->Size = Vector2(10,3);
        gadget->AddComponent<Sprite>()->SpriteName = "wood";
        gadget->AddComponent<Colorable>()->Color = Colour::White();
        gadget->AddComponent<Touchable>()->Click += event_handler(this, &TestGUI::GadgetClicked);
        gadget->AddComponent<Atlas>(atlas);
        gadget->AddComponent<TextureComponent>(atlas);
        gadget->GetComponent<Sprite>()->CornerSize = 0.3f;
        
            
        label = world.CreateObject();
        label->AddComponent<Transform>()->Position = Vector3(10,3,1) * 0.5f;
        label->AddComponent<Mesh>();
        label->AddComponent<Material>()->LightingEnabled = false;
        label->GetComponent<Material>()->BlendMode = BlendMode::Alpha;
        label->AddComponent<Font>()->Load("OtherFont.fnt");
        label->AddComponent<TextureComponent>()->Texture().LoadFromPng("OtherFont.png");
        label->AddComponent<Label>()->Text = "Click me";
        label->AddComponent<Sizeable>()->Size = Vector2(0,0);
        label->GetComponent<Label>()->WordWrap = false;
        label->GetComponent<Label>()->HAlignment = Font::Center;
        label->GetComponent<Label>()->VAlignment = Font::Middle;
        
    }
    
    void Update(float dt) {
        static float rotation = 0;
        rotation+=dt;
        
        Colour color = gadget->GetComponent<Colorable>()->Color;
        color.A(0.5f + sinf(rotation) * 0.5f);
        
        gadget->GetComponent<Colorable>()->Color = color;
        gadget->GetComponent<Transform>()->Rotation = rotation;
        
        label->GetComponent<Transform>()->Rotation = rotation;
        
        world.Update(dt);
    }
    
    void Render() {
        glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_CULL_FACE);
        renderer->Render();
    }
    
    void TouchUp(TouchEvent e) {
        //float amount = (e.Position.x / ScreenSize().x);
        // label->GetComponent<Sizeable>()->Size = Vector2(amount* 20, 0);
    }
    
    void GadgetClicked(TouchData t) {
        //t.Touchable->Owner()->GetComponent<Sprite>()->SpriteName = "Button";
        //t.Touchable->Owner()->GetComponent<Sizeable>()->Size *= 1.1f;
        // t.Touchable->Owner()->GetComponent<Transform>()->Anchor =  t.Touchable->Owner()->GetComponent<Sizeable>()->Size * 0.5f;
        //label->GetComponent<Sizeable>()->Size = t.Touchable->Owner()->GetComponent<Sizeable>()->Size;
    }
    
    
    GameWorld world;
    RenderSystem* renderer;
    GameObject* gadget;
    GameObject* label;
};

/*
int main() {
    Engine e;
    e.Start<TestGUI>();
}
*/




